package com.android.oc.firstapp;

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

class TypeTemp {
	private int id;
	private String content;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}

public class Input extends Activity implements
		AdapterView.OnItemSelectedListener, OnClickListener {

	private Type[] drop_selected = {};
	private String[] con = {};
	private SQLiteDatabase db = null;
	private Cursor constantsCursor = null;

	private EditText TaskContent = null;
	private TextView DateTime = null;
	private int level = 0;
	private int type = 0;

	// date and time
	private int mYear;
	private int mMonth;
	private int mDay;

	private ImageButton level_1 = null;
	private ImageButton level_2 = null;
	private ImageButton level_3 = null;
	private ImageButton level_4 = null;
	private ImageButton cancel = null;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.input);
		Bundle bundle = getIntent().getExtras();
		String action = bundle.getString("action");
	

		// create database

		db = (new SQLiteHelper(this)).getWritableDatabase();
		// add spinner
		iniSpinner(0);
		int position = 0;
		if(action.toString().equals("update")) {
			for(int i = 0; i < this.drop_selected.length; i++) {
				if(this.drop_selected[i].getId() == bundle.getInt("list_id")) {
					position = i;
					break;
				}
			}
			Spinner spin = (Spinner) findViewById(R.id.Spinner01);
			spin.setSelection(position);
		}
		
		// add save button
		ImageButton save = (ImageButton) findViewById(R.id.ok);
		if (action.toString().equals("add")) {
			save.setOnClickListener(saveTask);
		} else {
			save.setOnClickListener(updateTask);
		}

		// add TaskContent
		this.TaskContent = (EditText) findViewById(R.id.EditText01);

		this.TaskContent.setText(bundle.getString("content"));
	
		// add DateTime
		this.DateTime = (TextView) findViewById(R.id.TextView03);
		Button datetime = (Button) findViewById(R.id.Button_date);
		datetime.setOnClickListener(setTime);
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		if (action.toString().equals("update")) {
			this.DateTime.setText(bundle.getString("datetime"));
		} else {
			updateDisplay();
		}

		// add listener for type
		ImageButton addType = (ImageButton) findViewById(R.id.ImageButton_AddType);
		addType.setOnClickListener(this);
		//add listener for cancel
		cancel = (ImageButton) findViewById(R.id.close);
		cancel.setOnClickListener(delete);
		// default level
		this.level = 0;
		this.level_1 = (ImageButton) findViewById(R.id.ImageButton01);
		this.level_2 = (ImageButton) findViewById(R.id.ImageButton02);
		this.level_3 = (ImageButton) findViewById(R.id.ImageButton03);
		this.level_4 = (ImageButton) findViewById(R.id.ImageButton04);
		defaultImageButton();
		if (action.toString().equals("update")) {
			int l = bundle.getInt("level");
			TableRow.LayoutParams LP_WW = new TableRow.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			LP_WW.weight = 1;
			switch (l) {
			case 1:
				level_1.setImageResource(R.drawable.level1);
				level_1.setLayoutParams(LP_WW);
				level = 1;
				break;
			case 2:
				level_2.setImageResource(R.drawable.level2);
				level_2.setLayoutParams(LP_WW);
				level = 1;
				break;
			case 3:
				level_3.setImageResource(R.drawable.level3);
				level_3.setLayoutParams(LP_WW);
				level = 1;
				break;
			case 4:
				level_4.setImageResource(R.drawable.level4);
				level_4.setLayoutParams(LP_WW);
				level = 1;
				break;

			default:
				break;
			}

		}
		this.level_1.setOnClickListener(setLevel_1);
		this.level_2.setOnClickListener(setLevel_2);
		this.level_3.setOnClickListener(setLevel_3);
		this.level_4.setOnClickListener(setLevel_4);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (constantsCursor != null) {
			constantsCursor.close();
		}

		db.close();
	}

	View.OnClickListener saveTask = new OnClickListener() {
		public void onClick(View v) {
			ContentValues cv = new ContentValues();
			cv.put("content", TaskContent.getText().toString());
			cv.put("level", level);
			cv.put("status", 0);
			cv.put("datetime", DateTime.getText().toString());
			cv.put("list_id", type);
			db.insert("task_content", null, cv);
			Intent intent = new Intent(Input.this, FirstApp.class);
			startActivity(intent);
			finish();
		}
	};

	View.OnClickListener updateTask = new OnClickListener() {
		public void onClick(View v) {
			Bundle bunde = getIntent().getExtras();
			int id = bunde.getInt("_id");
			ContentValues update = new ContentValues();
			update.put("content", TaskContent.getText().toString());
			update.put("level", level);
			update.put("status", 0);
			update.put("datetime", DateTime.getText().toString());
			update.put("list_id", type);
			
			db.update("task_content", update, "_id=?", new String[] {Integer.toString(id)});
			Intent intent = new Intent(Input.this, FirstApp.class);
			startActivity(intent);
			finish();
		}
	};
	
	View.OnClickListener delete = new OnClickListener() {
		public void onClick(View v) {
			Bundle bunde = getIntent().getExtras();
			int id = bunde.getInt("_id");
			if(bunde.getString("action").toString().equals("update")) {
				db.delete("task_content", "_id=?", new String[] {Integer.toString(id)});
			}
			Intent intent = new Intent(Input.this, FirstApp.class);
			startActivity(intent);
			finish();
		}
	};

	View.OnClickListener setTime = new OnClickListener() {
		public void onClick(View v) {
			showDialog(0);
		}
	};

	View.OnClickListener setLevel_1 = new OnClickListener() {
		public void onClick(View v) {
			defaultImageButton();
			TableRow.LayoutParams LP_WW = new TableRow.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			LP_WW.weight = 1;
			level_1.setImageResource(R.drawable.level1);
			level_1.setLayoutParams(LP_WW);
			level = 1;
		}
	};
	View.OnClickListener setLevel_2 = new OnClickListener() {
		public void onClick(View v) {
			defaultImageButton();
			TableRow.LayoutParams LP_WW = new TableRow.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			LP_WW.weight = 1;
			level_2.setImageResource(R.drawable.level2);
			level_2.setLayoutParams(LP_WW);
			level = 2;
		}
	};
	View.OnClickListener setLevel_3 = new OnClickListener() {
		public void onClick(View v) {
			defaultImageButton();
			TableRow.LayoutParams LP_WW = new TableRow.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			LP_WW.weight = 1;
			level_3.setImageResource(R.drawable.level3);
			level_3.setLayoutParams(LP_WW);
			level = 3;
		}
	};
	View.OnClickListener setLevel_4 = new OnClickListener() {
		public void onClick(View v) {
			defaultImageButton();
			TableRow.LayoutParams LP_WW = new TableRow.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			LP_WW.weight = 1;
			level_4.setImageResource(R.drawable.level4);
			level_4.setLayoutParams(LP_WW);
			level = 4;
		}
	};

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		drop_selected[position].getId();
		this.type = drop_selected[position].getId();
		;
	}

	public void onNothingSelected(AdapterView<?> parent) {

	}

	@Override
	protected Dialog onCreateDialog(int id) {

		return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);

	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {

		((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);

	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};

	private void updateDisplay() {
		this.DateTime.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(mMonth + 1).append("-").append(mDay).append("-")
				.append(mYear));
	}

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	private void defaultImageButton() {
		TableRow.LayoutParams LP_WW = new TableRow.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT - 10);
		LP_WW.weight = 1;
		LP_WW.gravity = 17;
		level_1.setImageResource(R.drawable.level1_s);
		level_2.setImageResource(R.drawable.level2_s);
		level_3.setImageResource(R.drawable.level3_s);
		level_4.setImageResource(R.drawable.level4_s);
		this.level_1.setLayoutParams(LP_WW);
		this.level_2.setLayoutParams(LP_WW);
		this.level_3.setLayoutParams(LP_WW);
		this.level_4.setLayoutParams(LP_WW);
	}

	// add Type
	public void onClick(View v) {
		EditText type = (EditText) findViewById(R.id.EditText_addtype);
		if (type.getText().toString().equals("")) {
			Toast.makeText(Input.this, "Please input type!", Toast.LENGTH_LONG)
					.show();
		} else {
			ContentValues cv = new ContentValues();
			cv.put("listname", type.getText().toString());
			cv.put("status", 1);
			db.insert("task_list", null, cv);
			Toast.makeText(Input.this, "Add Type success!", Toast.LENGTH_LONG)
					.show();
			// update spinner
			iniSpinner(con.length);

		}

	}

	private void iniSpinner(int position) {
		String sql = "SELECT * FROM task_list";
		this.constantsCursor = this.db.rawQuery(sql, null);
		int CountType = this.constantsCursor.getCount();
		if (this.constantsCursor.getCount() != 0) {
			this.drop_selected = new Type[this.constantsCursor.getCount()];
			this.con = new String[this.constantsCursor.getCount()];
			this.constantsCursor.moveToFirst();
			int j = 0;
			while (!this.constantsCursor.isAfterLast()) {
				Type row = new Type();
				row.setId(this.constantsCursor.getInt(0));
				row.setContent(this.constantsCursor.getString(1));
				row.setStatus(this.constantsCursor.getInt(2));
				this.drop_selected[j] = row;
				this.con[j] = this.constantsCursor.getString(1).toString();
				this.constantsCursor.moveToNext();
				j++;
			}
		}
		Spinner spin = (Spinner) findViewById(R.id.Spinner01);
		spin.setOnItemSelectedListener((OnItemSelectedListener) this);
		ArrayAdapter<String> spiner_adp = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, con);
		spiner_adp
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(spiner_adp);
		if (position != 0 && this.constantsCursor.getCount() != 0) {
			spin.setSelection(position);
		}
	}

}
